package com.panshi.controller;

import com.panshi.api.IUserService;
import com.panshi.vo.ResponseVO;
import com.panshi.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@Api(tags = "用户登录接口")
public class LoginController {

    @Autowired
    IUserService userService; //用户service层
    /**
     * 登录验证
     * @param userName 登录邮箱
     * @param pwd      密码
     * @return
     */
    @PostMapping("/loginVali")
    @ApiOperation("用户登录验证")
    @ResponseBody
    public ResponseVO loginValidate(@RequestParam String userName,@RequestParam String pwd){
        ResponseVO rs=new ResponseVO();
        //调用api判断该邮箱是否存在
        boolean bool = userService.findEmailExist(userName);
        if(!bool){
            rs.setMsg("您输入的邮箱不存在！请先注册！");
            rs.setSuccess(false);
            rs.setState(500);
            return rs;
        }else{
            //判断账号密码是否匹配
            boolean b = userService.checkAccountAndPwd(userName,pwd);
            if(b){
                //给该用户存入最后登录时间
                UserVO loginUser = userService.findUserByEmail(userName);
                //指定时间数据格式
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                loginUser.setU_last_time(df.format(new Date()));
                userService.updateUserLastTime(loginUser);
                System.out.println(loginUser);
                rs.setMsg("登录成功！");
                rs.setSuccess(true);
                rs.setState(200);
                rs.setUserVO(userService.findUserByEmail(userName));
                System.out.println(rs);
                return rs;
            }else{
                rs.setMsg("您输入密码错误！请重新输入！");
                rs.setSuccess(false);
                rs.setState(500);
                return rs;
            }
        }
    }
    @RequestMapping(value = "/user/findUserByUid", method = RequestMethod.POST)
    public UserVO findUserByUid(@RequestParam String uid){

        return userService.findUserByuid(uid);
    }
    @RequestMapping(value = "/user/findUserByEmail", method = RequestMethod.POST)
    public UserVO findUserByEmail(@RequestParam String userName){

        return userService.findUserByEmail(userName);
    }
}
